<?php

namespace application\Controller;

use application\Comm\Func;
use application\Comm\Request;
use application\Components\BaseController;
use application\Module\Zxadimg;
use application\Module\Zxarticle;
use application\Module\Zxarticletag;
use library\Core\Config;
use application\Module\Adimage;
use application\Module\Product;

class DecorateController extends BaseController
{
    /**
     * 首页
     */
    public function actionIndex()
    {
			$zxarticlemodule = new Zxarticle();
			$select = [
				'id',
				'thumb',
				'name',
				'description',
				'create_time',	
			];
			//文章列表
			$zxadata = $zxarticlemodule->getList($select,10,1);
			foreach ($zxadata['data'] as $zxkey => &$zxval){
				$zxval['tag_name'] = Zxarticle::getInstance()->zxArticleTagList($zxval['id']);
				$zxval['create_time'] = date('Y-m-d H:i:s',$zxval['create_time']);
				$zxval['description'] = mb_substr($zxval['description'], 0,85,'utf-8').'……';
				$zxval['thumb'] = Func::imgUrl($zxval['thumb']);
			}
			//热门文章
			$hot_list = $zxarticlemodule->getHot(10,1,'desc');
			
			$this->render('decorate/index',[
				'hot_list' => $hot_list,
				'data' => $zxadata['data'],
				'page' => $zxadata['page'],
			]);
    }

//     /**
//      * 列表
//      */

//     public function actionList()
//     {
//         $zxarticleList = new Zxarticle();
//         $zxarticles = [];
//         $category = Config::getParamsVal('ZX_ARTICLE_CATEGORY');
//         $category_value = Config::getParamsVal('ZX_ARTICLE_CATEGORY_VALUE');
//         $cid = Request::get('cid');
//         $pid = strlen($cid) > 1 ? substr($cid, 0, 1) : '';
//         $category_name = isset($category_value[$cid]) ? $category_value[$cid] : '';
//         $category_pname = isset($category_value[$pid]) ? $category_value[$pid] : '';
//         $select = [
//             'id',
//             'name',
//             'description',
//             'like_num',
//             'category_id',
//             'url',
//             'create_time',
//         ];
//         $dataProvider = $zxarticleList->getList($select);
//         foreach ($dataProvider['data'] as $key => $zxarticle) {
//             $zxarticles[$key]['id'] = $zxarticle['id'];
//             $zxarticles[$key]['name'] = $zxarticle['name'];
//             $zxarticles[$key]['like_num'] = $zxarticle['like_num'];
//             $zxarticles[$key]['url'] = $zxarticle['url']?$zxarticle['url']:Func::createUrl('decorate/view',['id'=>$zxarticle['id']]);
//             $zxarticles[$key]['tag_list'] = Zxarticle::getInstance()->zxArticleTagList($zxarticle['id']);
//             $zxarticles[$key]['description'] = $zxarticle['description'];
//             $zxarticles[$key]['category_id'] = isset($category_value[$zxarticle['category_id']]) ? $category_value[$zxarticle['category_id']] : '';
//             $zxarticles[$key]['create_time'] = date('Y-m-d H:i:s', $zxarticle['create_time']);
//         }
//         $hot_list = Zxarticle::getInstance()->getHot(10);
//         $this->render('decorate/list', [
//             'data' => $zxarticles,
//             'category' => $category,
//             'page' => $dataProvider['page'],
//             'hot_list' => $hot_list,
//             'category_name' => $category_name,
//             'category_pname' => $category_pname,
//             'cid' => $cid,
//             'pid' => $pid,
//         ]);
//     }

    /**
     * @param $id
     * 详情
     */
    public function actionView($id = '')
    {
        if(!$id)$this->redirect(Config::getParamsVal('HOST'));
        $info = Zxarticle::getInstance()->getInfo($id);
        if (!$info) $this->redirect(Config::getParamsVal('HOST'));      
        
        //获取文章标签列表
        $tag_list = Zxarticle::getInstance()->zxArticleTagList($id);
        //热门文章
        $hot_list = Zxarticle::getInstance()->getHot(10,1,'desc');
		$info['create_time'] = date('Y-m-d H:i:s',$info['create_time']);
		//热门推荐
		$adimage = new Adimage();
		//要根据配置文件热销id来改变这里的热销id
		$hot_product = $adimage->getList(24);
		$hotId = [];
		foreach ($hot_product as $hk => $hv){
			$hotId[] = intval($hv['name']);
		}
		 
		$hot_data = [];
		if($hotId){
			$select=[
				'id',
				'name',
				'thumb',
				'is_show_price',
				'price',
				'price_id',
				'unit'
			];
			$productmodule = new Product();
			$hot_data = $productmodule->getListByIds($hotId,$select);
		}
		 
        $this->render('decorate/view', [
        	'hot_data' => $hot_data,
            'info' => $info,
            'tag_list' => $tag_list,
            'hot_list' => $hot_list,
        ]);
    }

    /**
     * 列表
     */

    public function actionTag()
    {
        $tid = Request::get('tid');
        if(!$tid)$this->redirect(Config::getParamsVal('HOST'));
        $tag_name = Zxarticletag::getInstance()->getTagName($tid);

        $category = Config::getParamsVal('ZX_ARTICLE_CATEGORY');
        $category_value = Config::getParamsVal('ZX_ARTICLE_CATEGORY_VALUE');
        $select = [
            'id',
            'zxarticle_id',
            'create_time',
        ];
        $dataProvider = Zxarticle::getInstance()->getTagList($select);
        foreach ($dataProvider['data'] as $key => $zxarticle) {
            $zxarticles[$key]['id'] = $zxarticle['id'];
            $zxarticles[$key]['name'] = $zxarticle['name'];
            $zxarticles[$key]['like_num'] = $zxarticle['like_num'];
            $zxarticles[$key]['url'] = Func::createUrl('decorate/view',['id'=>$zxarticle['zxarticle_id']]);
            $zxarticles[$key]['tag_list'] = Zxarticle::getInstance()->zxArticleTagList($zxarticle['zxarticle_id']);
            $zxarticles[$key]['description'] = $zxarticle['description'];
            $zxarticles[$key]['category_id'] = isset($category_value[$zxarticle['category_id']]) ? $category_value[$zxarticle['category_id']] : '';
            $zxarticles[$key]['create_time'] = date('Y-m-d H:i:s', $zxarticle['create_time']);
        }
        $hot_list = Zxarticle::getInstance()->getHot(10);
        $this->render('decorate/tag', [
            'data' => $zxarticles,
            'category' => $category,
            'page' => $dataProvider['page'],
            'hot_list' => $hot_list,
            'tag_name' => $tag_name,
        ]);
    }

    /**
     * 首页热门点赞
     */
    public function actionLike()
    {
        $ad_id = Request::get('id');
        $result = Zxadimg::getInstance()->Like($ad_id);
        echo $result?1:0;
    }

    /**
     * 文章点赞
     */
    public function actionTaglike()
    {
        $id = Request::get('id');
        $result = Zxarticle::getInstance()->Like($id);
        echo $result?1:0;
    }

}
